package spill;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;

public class Bucket extends Item {
	private double cash;
	private int offsetX;
	private int offsetY;
	private char[] name;
	private char[] progress;
	private int restX;
	private int restY;
	private int vX;
	private double theta;
	
	public Bucket(String name, Engine e) {
		super("Graphics/bucketC.txt", "Graphics/bucketD.txt", e);
		cash = 100;
		offsetX = 0;
		offsetY = 0;
		this.name = name.toCharArray();
		vX = 0;
		progress = new char[] {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'};
		theta = 0;
	}
	public void setRestCoord(int x, int y) {
		restX = x;
		restY = y;
		this.x = x;
		this.y = y;
	}
	
	public void eat(ArrayList<Money> money, Engine e) {
		for (int i = money.size()-1; i >= 0; i--) {
			if (isInside(money.get(i).getMidX(), money.get(i).getMidY())) {
				cash = Math.min(100, cash+10);
				e.remove(money.get(i));
			}
		}
	}
	public boolean isClicked(int x, int y) {
		if (isInside(x, y)) {
			offsetX = this.x-x;
			offsetY = this.y-y;
			return true;
		}
		return false;
	}
	private boolean isInside(int x, int y) {
		return (x > this.x && x < this.x + 100 && y > this.y && y < this.y + 70);
	}
	public void move(int x, int y) {
		theta = (vX-(this.x-x-offsetX))*0.05;
		this.x = x+offsetX;
		this.y = y+offsetY;
	}
	public void consume(long time) {
		cash = cash - Math.log10(time)*Math.random()/15;
	}
	public boolean isAlive() {
		return cash > 0;
	}
	public void rest() {
		x = restX;
		y = restY;
		vX = 0;
		theta = 0;
	}
	public int getMidX() {
		return x+16/2*7;
	}
	public void draw(Graphics g) {
		Graphics2D g2d = (Graphics2D) g.create();
		g2d.rotate(theta, getMidX(), y);
		super.draw(g2d);
		g2d.setColor(Color.green);
		int lengde = (int)Math.max(progress.length*(cash/100), 0);
		g2d.drawChars(progress, 0, lengde, x+10, y+40);
		g2d.setColor(Color.black);
		g2d.setFont(engine.getFont(1.5));
		g2d.drawChars(name, 0, name.length, x+10, y+20);
		g2d.dispose();
	}
}
